<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="(ruleForm.id !== 0 ? '修改' : '添加') + '设备'" v-model="isShowDialog" width="660px">
			<el-form ref="formRef" :model="ruleForm" :rules="rules" size="default" label-width="100px">
				<el-form-item label="设备名称" prop="name">
					<el-input v-model="ruleForm.name" placeholder="请输入产品名称" clearable maxlength="10"></el-input>
				</el-form-item>
				<el-form-item label="所属部门" prop="deptId">
					<el-cascader
						:options="deptTree"
						:props="{ checkStrictly: true, emitPath: false, value: 'deptId', label: 'deptName' }"
						placeholder="请选择所属部门"
						clearable
						v-model="ruleForm.deptId"
						style="width: 60%"
					>
						<template #default="{ node, data }">
							<span>{{ data.deptName }}</span>
							<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
						</template>
					</el-cascader>
				</el-form-item>
				<el-form-item label="所属产品" prop="productId">
					<el-select v-model="ruleForm.productId" placeholder="请选择所属产品" clearable>
						<el-option v-for="pl in productList" :key="'pl-' + pl.id" :label="pl.name" :value="pl.id"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="描述">
					<el-input v-model="ruleForm.describe" type="textarea" placeholder="请输入描述" maxlength="150" show-word-limit :rows="4"></el-input>
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ ruleForm.id !== 0 ? '修 改' : '添 加' }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, toRefs, unref } from 'vue';
import { ElMessage } from 'element-plus';

import { addDevice, editDevice } from '/@/api/iot/device';

interface RuleFormState {
	id: number;
	deptId: number;
	productId: string;
	name: string;
	describe: string;
}

interface DeviceState {
	isShowDialog: boolean;
	ruleForm: RuleFormState;
	rules: {};
}
export default defineComponent({
	name: 'deviceEdit',
	props: {
		deptTree: {
			type: Array,
			default: () => [],
		},
		productList: {
			type: Array,
			default: () => [],
		},
	},
	setup(prop, { emit }) {
		const formRef = ref<HTMLElement | null>(null);
		const validateDeptId = (rule: any, value: any, callback: any) => {
			if (value === 0) {
				callback(new Error('所属部门不能为空'));
			} else {
				callback();
			}
		};
		const state = reactive<DeviceState>({
			isShowDialog: false,
			ruleForm: {
				id: 0,
				deptId: 0,
				productId: '',
				name: '',
				describe: '',
			},
			//表单校验
			rules: {
				deptId: [
					{ required: true, message: '所属部门不能为空', trigger: 'blur' },
					{ validator: validateDeptId, trigger: 'blur' },
				],
				productId: [{ required: true, message: '所属产品不能为空', trigger: 'blur' }],
				name: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
			},
		});
		// 打开弹窗
		const openDialog = (row?: any) => {
			resetForm();
			if (row) {
				state.ruleForm = row;
			}
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
			emit('dataList');
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			const formWrap = unref(formRef) as any;
			if (!formWrap) return;
			formWrap.validate((valid: boolean) => {
				if (valid) {
					if (state.ruleForm.id === 0) {
						//添加
						addDevice(state.ruleForm).then(() => {
							ElMessage.success('添加成功');
							closeDialog(); // 关闭弹窗
						});
					} else {
						//修改
						editDevice(state.ruleForm).then(() => {
							ElMessage.success('修改成功');
							closeDialog(); // 关闭弹窗
						});
					}
				}
			});
		};
		const resetForm = () => {
			state.ruleForm = {
				id: 0,
				deptId: 0,
				productId: '',
				name: '',
				describe: '',
			};
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			formRef,
			...toRefs(state),
		};
	},
});
</script>
